import 'package:get/get.dart';
import 'package:xin_he/utils/xin_he_event_bus.dart';

import '../../../service/xin_he_reqest.dart';
import '../../../utils/xin_he_storage.dart';
import '../../../utils/xin_he_toast.dart';
import '../xin_he_bank_management/xin_he_bank_entity.dart';
import 'xin_he_mine_withdraw_state.dart';

class XinHeMineWithdrawLogic extends GetxController {
  final XinHeMineWithdrawState state = XinHeMineWithdrawState();

  final bankModel  = XinHeBankEntity().obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();


    bus.on(XinHeStorage.settingOrUpdateBankInfo,(arg){

      requestBanksInfo();

    });

    requestBanksInfo();

  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }


  withdraw() async {

      var userId = await XinHeLocalStorage.get(XinHeStorage.userId);
      Map<String,dynamic> ss = {
        'userId':userId,
        'bankId': 12554192253227008,
        'mode':'PAY_TO_BANK_CARD',
        'money': 0.01,
      };

      XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/withdrawalsRecord/saveWithdrawalsRecord',params: ss,isPostAndwwwForm: true);
      if(!result.result || result.data['code'] != 0) {
        XinHeToast.center(result.data['message']);
        return ;
      }

  }

  requestBanksInfo() async{

    var userId =  await XinHeLocalStorage.get(XinHeStorage.userId);

    Map<String,dynamic>map = {
      // 'orderStatus':type,
      'userId':userId
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/bankCard/findBankByUserAndIsDefault',params: map,isPost: false);

    if(!result.result|| result.data['code'] != 0){
      XinHeToast.center(result.data['message']);
      return ;
    }

    Map<String, dynamic> data = result.data['data']??{};
    if(data.isNotEmpty) {
     bankModel.value =  XinHeBankEntity.fromJson(data);
    }

  }

}
